package com.xiaoxi.open.codemodel.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xiaoxi.open.codemodel.common.enumeration.CodeModelDictEnums;
import com.xiaoxi.open.codemodel.common.enumeration.CodeModelSystemEnums;
import com.xiaoxi.open.codemodel.common.enumeration.CodeModelUserEnums;
import com.xiaoxi.open.codemodel.core.ICode;
import com.xiaoxi.open.codemodel.domain.BmsCodeModel;
import com.xiaoxi.open.codemodel.domain.dto.BMSCodeModelDTO;
import com.xiaoxi.open.codemodel.domain.vo.BMSCodeCorrDictVO;
import com.xiaoxi.open.codemodel.domain.vo.BMSCodeModelVO;
import com.xiaoxi.open.codemodel.service.BmsCodeModelService;
import com.xiaoxi.open.common.exception.BaseException;
import com.xiaoxi.open.common.response.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName BmsCodeModelController
 * @Description
 * @Author xiaoxi
 * @Date 2022-11-05
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/quick/api/v1/codeModel")
public class BmsCodeModelController {

    @Resource
    private BmsCodeModelService bmsCodeModelService;

    @Resource
    private Map<String, ICode<?>> codeMap;


    /**
     * 新增
     *
     * @param bmsCodeModelVO
     * @return
     */
    @PostMapping("/add")
    public ResponseData<?> add(@RequestBody BMSCodeModelVO bmsCodeModelVO) {
        try {
            bmsCodeModelService.add(bmsCodeModelVO);
            return ResponseData.ok(bmsCodeModelVO);
        } catch (BaseException e) {
            log.error(e.getMessage(), e);
            return ResponseData.fail(e);
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public ResponseData<?> delete(@PathVariable("id") Long id) {
        try {
            bmsCodeModelService.delete(id);
            return ResponseData.ok(id);
        } catch (BaseException e) {
            log.error(e.getMessage(), e);
            return ResponseData.fail(e);
        }
    }

    /**
     * 更新
     *
     * @param bmsCodeModelVO
     * @return
     */
    @PostMapping("/update")
    public ResponseData<?> update(@RequestBody BMSCodeModelVO bmsCodeModelVO) {
        try {
            bmsCodeModelService.update(bmsCodeModelVO);
            return ResponseData.ok(bmsCodeModelVO);
        } catch (BaseException e) {
            log.error(e.getMessage(), e);
            return ResponseData.fail(e);
        }
    }

    /**
     * 查询分页数据
     *
     * @param bmsCodeModelDTO
     * @param pageable
     * @return
     */
    @GetMapping("/findListByPage")
    public ResponseData<?> findListByPage(BMSCodeModelDTO bmsCodeModelDTO, Pageable pageable) {
        try {
            IPage<BMSCodeModelVO> result = bmsCodeModelService.findListByPage(bmsCodeModelDTO, pageable);
            return ResponseData.ok(result);
        } catch (BaseException e) {
            log.error(e.getMessage(), e);
            return ResponseData.fail(e);
        }
    }

    /**
     * ID查询
     *
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public ResponseData<?> findById(@PathVariable Long id) {
        try {
            BmsCodeModel bmsCodeModel = bmsCodeModelService.findById(id);
            return ResponseData.ok(bmsCodeModel);
        } catch (BaseException e) {
            log.error(e.getMessage(), e);
            return ResponseData.fail(e);
        }
    }


    @GetMapping("checkCodeType")
    public ResponseData<?> checkCodeType(String codeType, Long id) {

        Boolean repeat = bmsCodeModelService.checkCodeType(codeType, id);
        if (repeat) {
            return ResponseData.fail(CodeModelUserEnums.CODE_TYPE_EXIST);
        }
        return ResponseData.ok();

    }

    @GetMapping("checkCodeName")
    public ResponseData<?> checkCodeName(String codeName, Long id) {

        Boolean repeat = bmsCodeModelService.checkName(codeName, id);
        if (repeat) {
            return ResponseData.fail(CodeModelUserEnums.CODE_NAME_EXIST);
        }
        return ResponseData.ok();

    }

    @GetMapping("enableCodeModel/{id}")
    public ResponseData<?> enableCodeModel(@PathVariable Long id) {

        try {
            bmsCodeModelService.enableCodeModel(id);
        } catch (BaseException e) {
            log.error("更新启用状态失败：{}", e.getMessage());
            return ResponseData.fail(e);
        }
        return ResponseData.ok();

    }

    @GetMapping("getCodeCorrDictList")
    public ResponseData<?> getCodeCorrDictList(String codeType) {

        BmsCodeModel bmsCodeModel = bmsCodeModelService.getCodeModel(codeType);
        if (Objects.isNull(bmsCodeModel)) {
            return ResponseData.fail(CodeModelSystemEnums.CODE_TYPE_NOT_EXIST);
        }

        String seqType = bmsCodeModel.getSeqType();

        List<?> codeList = null;
        try {
            codeList = codeMap.get(seqType).getCodeList(bmsCodeModel);
        } catch (BaseException e) {
            log.error("未知错误：{}", e.getMessage());
            return ResponseData.fail(e);
        }

        BMSCodeCorrDictVO bmsCodeCorrDictVO = new BMSCodeCorrDictVO();
        BeanUtils.copyProperties(bmsCodeModel, bmsCodeCorrDictVO);
        bmsCodeCorrDictVO.setCodeList(codeList);

        return ResponseData.ok(bmsCodeCorrDictVO);
    }

    @PostMapping("compileRun")
    public ResponseData<?> compileRun(@RequestBody BmsCodeModel bmsCodeModel) {

        try {
            codeMap.get(CodeModelDictEnums.SEQ.getCode()).getCodeList(bmsCodeModel);
        } catch (BaseException e) {
            log.error("未知错误：{}", e.getMessage());
            return ResponseData.fail(e);
        }

        return ResponseData.ok();

    }

}
